<?php

defined('SYSPATH') or die('Access error!');

class Controller_Store_Ajax extends Controller
{

    public function __construct(Request $request)
    {
        parent::__construct($request);
        header('Content-Type', 'application/json');
        if (!Request::$is_ajax)
        {
            $this->request->redirect(Route::get('shop')->uri(array('action' => 'index')));
            return;
        }
    }

    public function action_category_sub()
    {
        $id = intval($_POST['id']);
        if ($id == 0)
        {
            exit;
        }
        $category_list_temp = Model_Category::factory('category')->get_category_list('parent_id=' . $id);
        $category_list = array();
        if (Arr::validate_arr($category_list_temp))
        {
            foreach ($category_list_temp as $key => $value)
            {
                if(isset($value['id'])){
                    $value['children'] = Model_Category::factory('category')->get_category_list('parent_id=' . $value['id']);
                }
                $category_list[] = $value;
            }
        }
        echo json_encode($category_list);
        exit;
    }

}